# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Oct  8 2012)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc
from   util import *

ID_OPEN = 1000
ID_CLOSE = 1001
ID_README = 1002
ID_ABOUT = 1003

###########################################################################
## Class MainFrame
###########################################################################

class MainFrame ( wx.Frame ):
	
	def __init__( self, parent ):
		wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Salary Mailer", pos = wx.DefaultPosition, size = wx.Size( 600,430 ), style = wx.DEFAULT_FRAME_STYLE|wx.ICONIZE|wx.TAB_TRAVERSAL, name = u"Salary Mailer" )

		self.Tips   = u''
		self.templetf = u''
		self.salaryf  = u''
		self.tips     = u''

                SetLogDlg(self)
                
                try:
                        self.cfgs = GetConfig()
                except Exception, e:
                        Alert(u'读取配置失败！\n%s' % e)
                        self.Close()
		
		self.SetSizeHintsSz( wx.Size( 600,430 ), wx.Size( -1,-1 ) )
		
		self.m_statusBar = self.CreateStatusBar( 1, wx.ST_SIZEGRIP, wx.ID_ANY )
		self.m_menubar1 = wx.MenuBar( 0 )
		self.menu_file = wx.Menu()
		self.menu_open = wx.MenuItem( self.menu_file, ID_OPEN, u"打开(&O)", wx.EmptyString, wx.ITEM_NORMAL )
		self.menu_file.AppendItem( self.menu_open )
		
		self.menu_file.AppendSeparator()
		
		self.menu_close = wx.MenuItem( self.menu_file, ID_CLOSE, u"关闭(&C)", wx.EmptyString, wx.ITEM_NORMAL )
		self.menu_file.AppendItem( self.menu_close )
		
		self.m_menubar1.Append( self.menu_file, u"文件(&F)" ) 
		
		self.menu_help = wx.Menu()
		self.menu_README = wx.MenuItem( self.menu_help, ID_README, u"使用说明(&R)", wx.EmptyString, wx.ITEM_NORMAL )
		self.menu_help.AppendItem( self.menu_README )
		
		self.menu_help.AppendSeparator()
		
		self.menu_about = wx.MenuItem( self.menu_help, ID_ABOUT, u"关于(&A)", wx.EmptyString, wx.ITEM_NORMAL )
		self.menu_help.AppendItem( self.menu_about )
		
		self.m_menubar1.Append( self.menu_help, u"帮助(&H)" ) 
		
		self.SetMenuBar( self.m_menubar1 )
		
		bSizer6 = wx.BoxSizer( wx.HORIZONTAL )
		
		m_sizer = wx.BoxSizer( wx.VERTICAL )
		
		m_sizer.SetMinSize( wx.Size( 600,360 ) ) 
		bSizer61 = wx.BoxSizer( wx.HORIZONTAL )
		
		bSizer7 = wx.BoxSizer( wx.VERTICAL )
		
		self.m_staticText5 = wx.StaticText( self, wx.ID_ANY, u"选择薪资模板：", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText5.Wrap( -1 )
		bSizer7.Add( self.m_staticText5, 0, wx.ALL|wx.EXPAND, 5 )
		
		self.m_choiceTempletChoices = []
                for (k, v) in self.cfgs[u'resource'].items():
                        k = unicode(k, u'GB2312')
                        v = unicode(v, u'GB2312')
                        self.m_choiceTempletChoices.append(u'%s(%s)' % (k, v))
		self.m_choiceTemplet = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, self.m_choiceTempletChoices, 0 )
		self.m_choiceTemplet.SetSelection( -1 )
		bSizer7.Add( self.m_choiceTemplet, 0, wx.ALL|wx.EXPAND, 5 )
		
		self.m_staticText51 = wx.StaticText( self, wx.ID_ANY, u"邮件主题：", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText51.Wrap( -1 )
		bSizer7.Add( self.m_staticText51, 0, wx.ALL, 5 )
		
		self.m_textSubject = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
		bSizer7.Add( self.m_textSubject, 0, wx.ALL|wx.EXPAND, 5 )
		self.m_textSubject.SetValue(self.cfgs[u'global'][u'subject'])
		
		self.m_staticText7 = wx.StaticText( self, wx.ID_ANY, u"备注信息：", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText7.Wrap( -1 )
		bSizer7.Add( self.m_staticText7, 0, wx.ALL|wx.EXPAND, 5 )
		
		self.m_textTips = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE|wx.TE_RICH2 )
		self.m_textTips.SetMinSize( wx.Size( -1,80 ) )
		
		bSizer7.Add( self.m_textTips, 0, wx.ALL|wx.EXPAND, 5 )
		
		self.m_staticText6 = wx.StaticText( self, wx.ID_ANY, u"选择薪资文件：", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText6.Wrap( -1 )
		bSizer7.Add( self.m_staticText6, 0, wx.ALL|wx.EXPAND, 5 )
		
		self.m_filePicker = wx.FilePickerCtrl( self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.xls;*.xlsx", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE )
		bSizer7.Add( self.m_filePicker, 0, wx.ALL|wx.EXPAND, 5 )
		
		
		bSizer61.Add( bSizer7, 1, wx.EXPAND, 5 )
		
		bSizer8 = wx.BoxSizer( wx.VERTICAL )
		
		self.m_staticText3 = wx.StaticText( self, wx.ID_ANY, u"日志：", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText3.Wrap( -1 )
		bSizer8.Add( self.m_staticText3, 0, wx.ALL, 5 )
		
		self.m_textlog = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( -1,-1 ), wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2 )
		self.m_textlog.SetMaxLength( 0 ) 
		self.m_textlog.SetMinSize( wx.Size( 320,240 ) )
		
		bSizer8.Add( self.m_textlog, 0, wx.ALL|wx.EXPAND, 5 )
		
		
		bSizer61.Add( bSizer8, 1, wx.EXPAND, 5 )
		
		
		m_sizer.Add( bSizer61, 1, wx.EXPAND, 5 )
		
		self.m_gauge = wx.Gauge( self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
		m_sizer.Add( self.m_gauge, 0, wx.ALL|wx.EXPAND, 5 )
		
		self.m_btnmailer = wx.Button( self, wx.ID_ANY, u"Mailer", wx.DefaultPosition, wx.DefaultSize, 0 )
		m_sizer.Add( self.m_btnmailer, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 5 )
		
                self.m_btnmailer.Disable()
		
		bSizer6.Add( m_sizer, 1, wx.EXPAND, 5 )
		
		
		self.SetSizer( bSizer6 )
		self.Layout()
		
		self.Centre( wx.BOTH )
		
		# Connect Events
		self.Bind( wx.EVT_CLOSE, self.OnClose )
		self.Bind( wx.EVT_SIZE, self.OnSize )
		self.Bind( wx.EVT_MENU, self.OnMenuOpen, id = self.menu_open.GetId() )
		self.Bind( wx.EVT_MENU, self.OnMenuClose, id = self.menu_close.GetId() )
		self.Bind( wx.EVT_MENU, self.OnMenuReadme, id = self.menu_README.GetId() )
		self.Bind( wx.EVT_MENU, self.OnMenuAbout, id = self.menu_about.GetId() )
		self.m_choiceTemplet.Bind( wx.EVT_CHOICE, self.OnTempletChange )
		self.m_textTips.Bind( wx.EVT_TEXT, self.OnTipsChange )
		self.m_filePicker.Bind( wx.EVT_FILEPICKER_CHANGED, self.OnFileChange )
		self.m_btnmailer.Bind( wx.EVT_BUTTON, self.onMailer )
	
	def __del__( self ):
		pass
	
	
	# Virtual event handlers, overide them in your derived class
	def OnClose( self, event ):
		event.Skip()
	
	def OnSize( self, event ):
		event.Skip()
	
	def OnMenuOpen( self, event ):
		event.Skip()
	
	def OnMenuClose( self, event ):
		event.Skip()
	
	def OnMenuReadme( self, event ):
		event.Skip()
	
	def OnMenuAbout( self, event ):
		event.Skip()
	
	def OnTempletChange( self, event ):
                self.templetf = u''
                n = self.m_choiceTemplet.GetCurrentSelection()
                m = re.match(r'.+\((.+)\)', self.m_choiceTempletChoices[n])
                if m:
                        self.templetf = m.group(1)
                        WrtLog(u'UI: 使用模板(%s)\n' % self.templetf)
		event.Skip()
	
	def OnTipsChange( self, event ):
                self.tips = self.m_textTips.GetValue()
		event.Skip()
	
	def OnFileChange( self, event ):
                if (self.templetf != u''):
                        self.salaryf = self.m_filePicker.GetPath()
                        ProcThread(self).start()
                else:
                        self.m_filePicker.SetPath(u'')
                        self.m_btnmailer.Disable()
                        Alert(u'请选择模板文件！')
		event.Skip()
	
	def onMailer( self, event ):
                SendThread(self).start()
		event.Skip()
	

